#include "e9.h"
#include "typedef.h"

char *hexchars = "0123456789abcdef";

void u32tohex(u8 *buf, u32 h)
{
  buf[0] = hexchars[(h >> 28) & 0xf];
  buf[1] = hexchars[(h >> 24) & 0xf];
  buf[2] = hexchars[(h >> 20) & 0xf];
  buf[3] = hexchars[(h >> 16) & 0xf];
  buf[4] = hexchars[(h >> 12) & 0xf];
  buf[5] = hexchars[(h >> 8) & 0xf];
  buf[6] = hexchars[(h >> 4) & 0xf];
  buf[7] = hexchars[(h >> 0) & 0xf];
  buf[8] = 0;
}

void kprintf(char *format, ...)
{
  char **arg = (char **)&format;
  char c;
  char buf[32];
  char *p;
  arg++;
  while((c = *format++)!= 0)
  {
    if(c != '%')
    {
      e9_putc(c);
    }
    else
    {
      c = *format++;
      switch (c)
      {
        case 'x':
          u32tohex((u8*) buf, *((u32 *) arg++));
          p = buf;
          while(*p) e9_putc(*p++);
          break;
        case 'p':
          u32tohex((u8*) buf, *((u32 *) arg++));
          p = buf;
          e9_putc('0');
          e9_putc('x');
          while(*p) e9_putc(*p++);
          break;
        case 's':
          p = *arg++;
          if (!p) p = "(null)";
          while(*p) e9_putc(*p++);
          break;
        default:
          break;
      }
    }
  }
}

